package org.dark.bigdatareader.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.dark.bigdatareader.api.UserService;
import org.dark.bigdatareader.dao.mapper.User2Mapper;
import org.dark.bigdatareader.dao.mapper.UserMapper;
import org.dark.bigdatareader.service.IUserService;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Component
@RestController
public class UserController {

    @Autowired
    IUserService userService;

    @Autowired
    ApplicationContext context;
    @Autowired
    UserMapper userMapper;


    @Autowired(required = false)
    User2Mapper userMapper2;


    @Autowired
    public SqlSessionTemplate sqlSession;
//    @Autowired
//    Map<String, MybatisMapperProxy> mapperProxyMap;


    @GetMapping("/list")
    @ResponseBody
    public String getAll() {
//        MapperFactoryBean mapperFactoryBean = context.getBean(MapperFactoryBean.class);
//        mapperFactoryBean.isSingleton();
        BaseMapper mapper1 = sqlSession.getMapper(UserMapper.class);
        try {
            BaseMapper mapper2 = sqlSession.getMapper(User2Mapper.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        sqlSession.getConfiguration().addMapper(User2Mapper.class);
        BaseMapper mapper3 = sqlSession.getMapper(User2Mapper.class);
//        mapperProxyMap.size();
//        userMapper.selectById("2");
        mapper1.selectCount(new LambdaQueryWrapper());
        mapper3.selectCount(new LambdaQueryWrapper());
        try {
            userMapper2.selectCount(new LambdaQueryWrapper<>());
        } catch (Exception e) {
            e.printStackTrace();
        }

        UserService service = new UserService() {
            @Override
            public String getAll() {
                return userService.getAll().toString();
            }
        };
        return service.getAll();
    }
}
